<?php

namespace app\admin\controller\subject;

use app\common\controller\Backend;

/**
 * 课程管理 
 */
class Subject extends Backend
{
    /**
     * 是否是关联查询
     */
    protected $relationSearch = true;
    public function _initialize()
    {
        parent::_initialize();
        $this->model = model('Subject.Subject');
        $this->Categorymodel = model('Subject.Category');
        $category= $this->Categorymodel->order('weight','desc')->select();
        $data=[];
        foreach($category as $item){
            $data[$item['id']]=$item['name'];
        }
        $this->assign('category',$data);
    }
    /**
     * 查看
     */
    public function index()
    {
        if ($this->request->isAjax()) {
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $total = $this->model
                ->with('category')
                ->where($where)
                ->order($sort, $order)
                ->count();

            $list = $this->model
                ->with('category') 
                ->where($where)
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->select();
            $result = array("total" => $total, "rows" => $list);
            return json($result);
        }
        return $this->view->fetch();
    }
    /**
     * 添加
     */
    public function add()
    {   
        if($this->request->isPost()){
            $params=$this->request->post('row/a');
            if(empty($params)){
                $this->error("参数不能为空");
            }
            $result=$this->model->validate('common/Subject/Subject')->save($params);
            if(!$result){
                $this->error($this->model->getError());
            }
            $this->success();
        }
        return $this->view->fetch();
    }
    /**
     * 编辑
     */
    public function edit($ids = null)
    {    $row=$this->model->find($ids);
        if(!$row){
            $this->error(__('当前课程不存在'));
        }
        if($this->request->isPost()){
            $params=$this->request->post('row/a');
            $params['id']=$ids;
            if(empty($params)){
                $this->error("参数不能为空");
            }
            $result=$this->model->validate('common/Subject/Subject.edit')->isUpdate()->save($params);
            if(!$result){
                // 判断图片是否有上传
                if (!empty($params['thumbs']) && $params['thumbs'] != $row['thumbs']) {
                    is_file(ltrim($params['thumbs'], '/')) && unlink(ltrim($params['thumbs'], '/'));
                }
                $this->error($this->model->getError());
            }  
            // 判断图片是否有上传
            if (!empty($params['thumbs']) && $params['thumbs'] != $row['thumbs']) {
                is_file(ltrim($row['thumbs'], '/')) && unlink(ltrim($row['thumbs'], '/'));
            }
            $this->success();
        }
    
        $this->assign('row',$row);
        return $this->view->fetch();
    }
     /**
     * 软删除
     */
    public function del($ids = null)
    {
        $categoryList=$this->model->all($ids);
        if(empty($categoryList)){
            $this->error('当前课程不存在');
        }
        $result=$this->model->destroy($ids);
        if(!$result){
            $this->error($this->model->getError());
        }
        $this->success();
    }
    // 分类列表
    public function category(){
        $category= $this->Categorymodel->order('weight','desc')->select();
        $data=[];
        foreach($category as $item){
            $data[$item['name']]=$item['name'];
        }
        echo json_encode($data);
    }
}
